package com.example.mytestapplication;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wei on 2017/6/23.
 */

public class MywidgetProvider extends AppWidgetProvider {
    public static final String TAG = "MywidgetProvider";
    public static final String IMG_BTN1 = "IMG_BTN1.CLICK";
    public static final String IMG_BTN2 = "IMG_BTN2.CLICK";
    private UdpHelper udpHelper;

    //当判断到是该事件发过来时， 我们就获取插件的界面
    @Override
    public void onReceive(final Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.d(TAG, "onReceive: ");
//        Toast.makeText(context, ""+intent.getAction(), Toast.LENGTH_SHORT).show();
        if(IMG_BTN1.equals(intent.getAction())){
//            Toast.makeText(context, "执行回家场景", Toast.LENGTH_SHORT).show();
//            startThread();
            updataViewForCome(context);
//            UdpHelper.getInstance(context).sendThread("cmd=810004337,N1#");
//            UdpHelper.getInstance(context).sendThread("cmd=810000568,N1#");
//            UdpHelper.getInstance(context).sendThread("cmd=510000024,P1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010361,N3#");
            UdpHelper.getInstance(context).sendThread("cmd=735010361,N1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010183,N2#");
            UdpHelper.getInstance(context).sendThread("cmd=510000026,P1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010546,F1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010546,F2#");
            UdpHelper.getInstance(context).sendThread("cmd=735010546,F3#");
            UdpHelper.getInstance(context).sendThread("cmd=735010183,F1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010183,F3#");
            UdpHelper.getInstance(context).sendThread("cmd=735010361,F2#");
            UdpHelper.getInstance(context).sendThread("cmd=120010442,A12#");
//            UdpHelper.getInstance(context).sendThread("cmd=715000008,N1#");
//            context.sendBroadcast(new Intent("IMG_BTN11.CLICK"));
        }else if(IMG_BTN2.equals(intent.getAction())){
//            Toast.makeText(context, "执行离家场景", Toast.LENGTH_SHORT).show();
//            startThread2();
            updataViewForBack(context);
//            UdpHelper.getInstance(context).sendThread("cmd=810004337,F1#");
//            UdpHelper.getInstance(context).sendThread("cmd=810000568,F1#");
//            UdpHelper.getInstance(context).sendThread("cmd=510000024,Q1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010361,F3#");
            UdpHelper.getInstance(context).sendThread("cmd=735010361,F1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010183,F2#");
            UdpHelper.getInstance(context).sendThread("cmd=510000026,Q1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010546,F1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010546,F2#");
            UdpHelper.getInstance(context).sendThread("cmd=735010546,F3#");
            UdpHelper.getInstance(context).sendThread("cmd=735010183,F1#");
            UdpHelper.getInstance(context).sendThread("cmd=735010183,F3#");
            UdpHelper.getInstance(context).sendThread("cmd=735010361,F2#");
            UdpHelper.getInstance(context).sendThread("cmd=120010442,A20#");
//            UdpHelper.getInstance(context).sendThread("cmd=715000008,F1#");
//            context.sendBroadcast(new Intent("IMG_BTN22.CLICK"));
        }else if("SEND_OK".equals(intent.getAction())){
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    updataViewsComplete(context);
                }
            };

            timer.schedule(task,3000);
        }
    }

    private void updataViewForBack(Context context) {
        AppWidgetManager instance = AppWidgetManager.getInstance(context);
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.layout_widget_provider);
        views.setImageViewResource(R.id.img_btn2,R.drawable.starting_leave_scan);
        ComponentName componentName = new ComponentName(context,MywidgetProvider.class);
        instance.updateAppWidget(componentName,views);
    }

    private void updataViewForCome(Context context) {
        AppWidgetManager instance = AppWidgetManager.getInstance(context);
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.layout_widget_provider);
        views.setImageViewResource(R.id.img_btn1,R.drawable.starting_come_scan);
        ComponentName componentName = new ComponentName(context,MywidgetProvider.class);
        instance.updateAppWidget(componentName,views);
    }

    private void updataViewsComplete(Context context){
        AppWidgetManager instance = AppWidgetManager.getInstance(context);
        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.layout_widget_provider);
        views.setImageViewResource(R.id.img_btn1,R.drawable.come_back);
        views.setImageViewResource(R.id.img_btn2,R.drawable.leave_back);
        ComponentName componentName = new ComponentName(context,MywidgetProvider.class);
        instance.updateAppWidget(componentName,views);
    }


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.d(TAG, "onUpdate: ");
        context.startService(new Intent(context,RecordService.class));
        for(int appWidgetId : appWidgetIds){
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout_widget_provider);
//        Intent clickIntent = new Intent(context, OneKeyOpenActivity.class);
            Intent imgBtnClick1 = new Intent(IMG_BTN1);
//            imgBtnClick1.putExtra("type","come");
            Intent imgBtnClick2 = new Intent(IMG_BTN2);
//            imgBtnClick2.putExtra("type","leave");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,imgBtnClick1,0);
            PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,0,imgBtnClick2,0);
            views.setOnClickPendingIntent(R.id.img_btn1,pendingIntent);
            views.setOnClickPendingIntent(R.id.img_btn2,pendingIntent2);
//            Toast.makeText(context, "添加view", Toast.LENGTH_SHORT).show();
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.d(TAG, "onEnabled: ");
//        Toast.makeText(context, "被创建", Toast.LENGTH_SHORT).show();
        UdpHelper udpHelper = UdpHelper.getInstance(context);
        if(!udpHelper.checkIsConnect()){
            udpHelper.sendThread("qip");
        }
    }






}
